<template>
  <div class="page-search">
    <YZFrom v-bind="searchFormConfig" v-model="formDate">
      <template #footer>
        <div class="handle-btns">
          <el-button @click="handleResetClick">
            <el-icon><refresh /></el-icon>
            重置
          </el-button>
          <el-button type="primary" @click="handleQueryClick">
            <el-icon> <search /></el-icon>
            搜索
          </el-button>
        </div>
      </template>
    </YZFrom>
  </div>
</template>

<script lang="ts">
import YZFrom from '@/baseUi/from'
// import eventBus from '@/utils/eventBus'
import { defineComponent, ref } from 'vue'

export default defineComponent({
  components: { YZFrom },
  props: {
    searchFormConfig: {
      type: Object,
      required: true
    }
  },
  setup(props, { emit }) {
    // 双向绑定的属性应该是由配置文件的field来决定的

    //1.优化:formData中的属性应该动态来决定
    const formItems = props.searchFormConfig?.formItems ?? []

    const formOriginData: any = {}

    for (const item of formItems) {
      formOriginData[item.field] = ''
    }
    // 2.当用户点击了重置的时候
    const formDate = ref(formOriginData)
    // 重置事件
    const handleResetClick = () => {
      for (const key in formOriginData) {
        formDate.value[key] = formOriginData[key]
      }
      emit('handleResetClick')
    }
    //3. 查询事件
    const handleQueryClick = () => {
      emit('handleQueryClick', formDate.value)
      // eventBus.emit('getPageData', formDate.value)
    }

    return { formDate, handleResetClick, handleQueryClick }
  }
})
</script>

<style lang="scss" scoped>
.header {
  color: red;
}
.handle-btns {
  text-align: right;
  padding: 0 50px 20px 0;
}
</style>
